package com.zxinlee.account.tx;

import com.zxinlee.account.entity.AccountMessage;
import com.zxinlee.account.service.AccountService;
import com.zxinlee.account.utli.JsonUtil;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(
        consumerGroup = "account-group",
        topic = "orderTopic")
public class TxConsumer implements RocketMQListener<String> {

    @Autowired
    private AccountService accountService;

    @Override
    public void onMessage(String message) {
        AccountMessage accountMessage = JsonUtil.from(message, AccountMessage.class);
        accountService.decrease(accountMessage.getUserId(),accountMessage.getMoney());
    }
}
